﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Doo");

Doo.Pager = function (element) {
    Doo.Pager.initializeBase(this, [element]);
}

Doo.Pager.Items = new Array();

Doo.Pager.prototype = {
    initialize: function () {
        Doo.Pager.callBaseMethod(this, 'initialize');
        this._itemindex = Doo.Pager.Items.length;
        Doo.Pager.Items.push(this);
        // Add custom initialization here

        var pager = this;
        if (this.triggerchangeoninit) {
            Sys.Application.add_load(function () {
                pager.set_pageindex(pager.pageindex);
            });
        }
        this.render();
    },
    dispose: function () {
        //Add custom dispose actions here
        Doo.Pager.callBaseMethod(this, 'dispose');
    },

    _itemindex: 0,

    pageindex: 0,
    set_pageindex: function (value, trig) {
        ///<param name="value" description="0 base" />
        this.pageindex = value;
        if (trig === false) return;
        var phandler = this.get_events().getHandler("pageindexchange");
        if (phandler != null) {
            phandler(this, Sys.EventArgs.Empty);
        }
    },
    get_pageindex: function () { return this.pageindex; },
    pagesize: 99999,
    set_pagesize: function (value) { this.pagesize = value; },
    get_pagesize: function () { return this.pagesize; },
    style: 0,
    set_style: function (value) { this.style = value; },
    get_style: function () { return this.style; },
    recordcount: 0,
    set_recordcount: function (value) { this.recordcount = value; },
    get_recordcount: function () { return this.recordcount; },

    get_pagecount: function () { return this.recordcount == 0 ? 0 : parseInt((this.recordcount - 1) / this.pagesize) + 1; },

    behavior: 0,
    set_behavior: function (value) { this.behavior = value; },
    get_behavior: function () { return this.behavior; },

    alias: null,
    set_alias: function (value) { this.alias = value; },
    get_alias: function () { return this.alias; },

    get_rowstart: function () { return this.pageindex * this.pagesize; },
    get_rowcount: function () { return this.pagesize; },
    get_events: function () {
        if (!this._events) {
            this._events = new Sys.EventHandlerList();
        }
        return this._events;
    },

    add_pageindexchange: function (handler) { this.get_events().addHandler("pageindexchange", handler); },
    remove_pageindexchange: function (handler) { this.get_events().removeHandler("pageindexchange", handler); },

    triggerchangeoninit: false,
    set_triggerchangeoninit: function (value) { this.triggerchangeoninit = value; },
    get_triggerchangeoninit: function () { return this.triggerchangeoninit; },

    render_item: function (index, current, txt) {
        return String.format("<a href='javascript:;' onclick='Doo.Pager.Items[{0}].set_pageindex({1})' class='{3}'>{2}</a>", this._itemindex, index - 1, txt ? txt : index, index == current ? "current" : "");
    },

    render: function () {
        //Render HTML
        var sb = new Sys.StringBuilder();
        while (this.get_pagecount() > 0) {
            var index = this.pageindex + 1;
            var max = this.get_pagecount();

            //Render Prev
            if (this.style == 2 || this.style == 1) {//PrevNext And Both
                if (index > 1) sb.append(this.render_item(index - 1, index, '上一页'));
            };

            //1.Render 1
            sb.append(this.render_item(1, index));

            //2.Render ...
            if (max < 2) break;
            var st = index - 1;
            var en = index + 1;
            if (en > max - 1) {
                st = max - 3;
                en = max - 1;
            }
            if (st < 2) {
                st = 2;
                en = 4;
            }
            if (en > max) en = max;

            if (st > 2) sb.append("...");

            //3.Render Middle
            for (var i = st; i <= en; i++) {
                sb.append(this.render_item(i, index));
            }

            //4.Render ...
            if (en == max) break;
            if (en < max - 1) sb.append("...");

            //5.Render Last
            sb.append(this.render_item(max, index));

            break;
        };
        //Render Next
        if (this.pageindex < this.get_pagecount()) {
            if (this.style == 2 || this.style == 1) {//PrevNext And Both
                if (index < max) sb.append(this.render_item(index + 1, index, '下一页'));
            };
        }
        
        this._element.innerHTML = sb.toString();
    }
}

Doo.Pager.registerClass('Doo.Pager', Doo.ControlBase);
